﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;
using Chinaer.Model.Common;

namespace Chinaer.Bll
{
    public class Customer :Repository, IBll.ICustomer
    {
        IDao.ICustomerDAO customerDAO = new Dao.CustomerDAO();

        public DTO.CustomerDTO GetByName(string customerName)
        {
            Model.Entities.Model_Customer model_Customer = customerDAO.GetByName(customerName);
            DTO.CustomerDTO customerDTO = new DTO.CustomerDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Customer, customerDTO);
            return customerDTO;
        }

        public DTO.CustomerDTO GetByApiToken(string apiToken)
        {
            string customerName = Chinaer.Core.Helper.ApiTokenHelper.GetUserNameWithApiToken(apiToken);
            DTO.CustomerDTO customerDTO = GetByName(customerName);
            return customerDTO;
        }

        public bool CheckApitokenIsUseful(string apiToken)
        {
            DTO.CustomerDTO customerDTO = GetByApiToken(apiToken);
            if (customerDTO == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        public List<DTO.CustomerDTO> GetAllCustomers(string apiToken)
        {
            List<DTO.CustomerDTO> listCustomer = new List<DTO.CustomerDTO>();
            List<Model.Entities.Model_Customer> listModelCustomer = new List<Model.Entities.Model_Customer>();
            try
            {
                listModelCustomer = customerDAO.GetAllCustomers();
                if (listModelCustomer != null && listModelCustomer.Count > 0)
                {
                    foreach (var item in listModelCustomer)
                    {
                        DTO.CustomerDTO administratorDTO = new DTO.CustomerDTO();
                        Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, administratorDTO);
                        listCustomer.Add(administratorDTO);
                    }
                }
            }
            catch (Exception ex)
            {

                listCustomer = null;
            }
            return listCustomer;
        }

        public Model.Common.JsonResult Add(DTO.CustomerDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            if (!string.IsNullOrEmpty(t.LoginName))
            {
                if (customerDAO.GetByName(t.LoginName) != null)
                {
                    jsonResult.Success = false;
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                    jsonResult.ErrorMessage = "该用户已经存在";
                    return jsonResult;
                }
            }
            try
            {
                Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Customer);
                if (model_Customer.ID.CheckIsNull().Success == false)
                {
                    model_Customer.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
                }
                jsonResult.Success = customerDAO.Add(model_Customer);
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(DTO.CustomerDTO t)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                if (t.CheckIsNull().Success == false)
                {
                    return t.CheckIsNull();
                }
                Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Customer);
                jsonResult.Success = customerDAO.Delete(model_Customer);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(string id)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                if (id.CheckIsNull().Success == false)
                {
                    return id.CheckIsNull();
                }
                jsonResult.Success = customerDAO.Delete(id);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Update(DTO.CustomerDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();

            try
            {
                Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Customer);
                jsonResult.Success = customerDAO.Update(model_Customer);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public DTO.CustomerDTO GetByID(string id)
        {
            DTO.CustomerDTO customerDTO = new DTO.CustomerDTO();

            try
            {
                if (id.CheckIsNull().Success == false)
                {
                    return null;
                }
                Model.Entities.Model_Customer model_Customer = new Model.Entities.Model_Customer();
                model_Customer = customerDAO.GetByID(id);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Customer, customerDTO);
            }
            catch (Exception ex)
            {
                customerDTO = null;
            }
            return customerDTO;
        }
    }
}
